package com.delio.server.reportes;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;


import com.delio.client.reportes.ReporteService;
import com.delio.server.base.Conexion;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ReporteServiceImpl extends RemoteServiceServlet 
implements ReporteService {

	private static final long serialVersionUID = 1L;
	
	@Override
	public String getPdfReport(String fileName, HashMap<String, Date> param) {
		System.out.println("server");
		
		String filePath = getServletConfig().getServletContext().getRealPath(fileName);		
		try {		
			System.out.println("entro en getPdfReport********************");
			JasperPrint print = getJasperPrint(fileName, param);
			
			String newFileName = filePath+".pdf";
			JasperExportManager.exportReportToPdfFile(print, newFileName);			
			
		} catch (SQLException e) {			
			e.printStackTrace();
		}catch (JRException e) {			
			e.printStackTrace();
		}
		System.out.println("fileName..."+fileName);
		return fileName+".pdf";
	}

	
	private JasperPrint getJasperPrint(String fileName, HashMap<String, Date> param) throws SQLException, JRException{
		
		String filePath = getServletConfig().getServletContext().getRealPath(fileName);
		Connection con;
		JasperPrint print=null;
		
		con = Conexion.getSqlMap().getDataSource().getConnection();
		
	/*	Pedido ped = new Pedido();
		ped.setId(9);
		
		preConsultas(ped.getId(), param);
		
		param.put("pedido_id", ped.getId());*/
		Reportes.setReportePedidos((Date)param.get("desde"), (Date)param.get("hasta"));
		System.out.println("filePath-----------"+filePath);
		print = JasperFillManager.fillReport(filePath+".jasper", param, con);

		return print;		
	}

}
